<?php
namespace app\api\controller\deerhome\user;

use app\api\controller\deerhome\user\Base;
use think\Db;
use app\admin\model\deerhome\Card as CardModel;
/**
 * 首页接口
 */
class Index extends Base
{
    /**
     * 首页
     *
     */
    public function index()
    {
        $home=Db::name("deerhome_design")->where("page","home")->find();
        $home=json_decode($home['con'],true);
        
        foreach ($home as $k => &$v) {
            if(isset($v['json'])){
                $v['json']=\json_decode($v['json'],true);
            }
        }
        // dump($home);
        $data=[];
        $data['guarantee']=[];
        if(isset($home['guarantee'])){
            $data['guarantee']= array_slice($home['guarantee'], 3, 1);
        }
        $data['tile']=[
//            ['link'=>'/pages/mall','image'=>$this->fixImg('/assets/addons/deerhome/img/tile_pjbh.png')],
//            ['link'=>'/pages/card/card','image'=>$this->fixImg('/assets/addons/deerhome/img/tile_tcck.png')],
//            ['link'=>'/pages/coupon/coupon','image'=>$this->fixImg('/assets/addons/deerhome/img/tile_yhq.png')]
        ];
        $data['showLogo']=2;
        $data['logo']="";
        $data['title']=$this->getXcxName();
        if(isset($home['title'])){
            $data['showLogo']=$home['title']['logo_show'];
            $data['title_logo']=$this->fixImg($home['title']['logo']);
            $data['title']=$home['title']['text'];
        }
        $data['notice']=isset($home['notice'])?$home['notice']:"";
        $data['keyword']=$home['tags'];
        $data['slide']=[];
        $ad_imgs=explode(",",$home['slide']['img']);
        // dump($home);
        // dump($ad_imgs);
        foreach ($ad_imgs as $key => $value) {
            $ad_slide=[];
            $ad_slide=$home['slide']['json'][$key];
            $ad_slide['img']=$this->fixImg($value);
            if(!\preg_match("/^#[0-9a-z]{6}$/i", $ad_slide['color'])){
                $ad_slide['color']="";
            }
            array_push($data['slide'],$ad_slide);
        }
        $data['fastac']=[];
        $data['fastac']['show']=(int)$home['block']['show'];
        $data['fastac']['title']=$home['block']['title'];
        $data['fastac']['desc']=$home['block']['desc'];
        $data['fastac']['list']=[];
        $fastac_imgs=explode(",",$home['block']['img']);
        foreach ($fastac_imgs as $key => $value) {
            $fastac=[];
            $fastac=$home['block']['json'][$key];
            $fastac['img']=$this->fixImg($value);
            array_push($data['fastac']['list'],$fastac);
        }
        $data['banner_center']=["img"=>""];
        if(count($home['banner_center']['json'])>0){
            $data['banner_center']=$home['banner_center']['json'][0];
            $data['banner_center']['img']=$this->fixImg($home['banner_center']['img']);
        }
        $data['banner_bottom']=["img"=>""];
        if(count($home['banner_bottom']['json'])>0){
            $data['banner_bottom']=$home['banner_bottom']['json'][0];
            $data['banner_bottom']['img']=$this->fixImg($home['banner_bottom']['img']);
        }
  
        $data['category']=Db::name("deerhome_cate")->field("id,name,face_image as img,cate_type")->where("cate_id",0)->where("home",1)->where("status",1)->order("weigh asc")->limit(15)->select();
        foreach ($data['category'] as &$item) {
            $item['img']=$this->fixImg($item['img']);
            $item['son']=Db::name("deerhome_cate")->where("cate_id",$item['id'])->where("status",1)->column("id");
        }
        $data['list']=[];
        foreach ($data['category'] as $value) {
            $arr=[];
            $arr['id']=$value['id'];
            $arr['name']=$value['name'];
            $arr['items']=Db::name("deerhome_items")->field("id,name,face_image,price,dw")->where("cate_id","in",$value['son'])->where("home",1)->where("status",1)->order("weigh asc")->limit(6)->select();
            foreach ($arr['items'] as &$v) {
                $v['face_image']=$this->fixImg($v['face_image'],true);
                $v['price']=$v['price']*1;
                $v['dw']=$v['dw']?"/".$v['dw']:"";
            }
            array_push($data['list'],$arr);
        }
        $data['category'] = [];
        $data['hide_copy']=0;

         //判断是否有会员折扣
         $discount=1;

        if($this->_user && isset($this->_user['id'])){
            Db::name("deerhome_user")->where("id",$this->_user['id'])->update(['last_time'=>date("Y-m-d H:i:s")]);

            if($this->_user['user_lev_id']>0){
                $discount=Db::name("deerhome_user_lev")->where("id",$this->_user['user_lev_id'])->value("discount");
                if(!$discount){
                    $discount=1;
                }else{
                    $discount=bcdiv($discount,100,2);
                }
            }
        }
        $data['card']=CardModel::where("is_home",1)->where("status",1)->limit(6)->select();
        foreach($data['card'] as &$v){
            if($v['vip_discount']==1){
                $v['price']=bcmul($v['price'],$discount,2);
            }
            $v['price_save']=bcsub($v['price_market'],$v['price'],0);
        }
        $this->xcxLog("首页");
        $this->ok($data);
    }
    /**
     * 商城
     *
     */
    public function mall()
    {
        $data=[];
        $data['banner']=[];
        $home=Db::name("deerhome_design")->where("page","mall")->find();
        if($home){
            $home=json_decode($home['con'],true);
            $banner=$home['banner'];
            foreach ($banner as $key => $value) {
                $data['banner'][]=[
                    'face_image'=>$this->fixImg($value['img']),
                    'banner_image'=>$this->fixImg($value['img']),
                    'banner_title'=>$value['title'],
                    'banner_desc'=>$value['desc'],
                    'id'=>$value['id'],
                    'name'=>$value['name'],
                    'price'=>$value['price'],
                ];
            }
        }
        $data['cate']=Db::name("deerhome_cate")->field("id,name,face_image")->where("cate_type",2)->where("cate_id",">",0)->where("status",1)->order("weigh asc")->select();
        foreach ($data['cate'] as &$item) {
            $item['face_image']=$this->fixImg($item['face_image']);
        }
        //在数组开头插入一个元素
        array_unshift($data['cate'],["id"=>0,"name"=>"精选推荐","face_image"=>$this->fixImg("/uploads/20250215/12252563a00fee1ecc045b4d01231200.png")]);
        $this->xcxLog("商城页");
        $this->ok($data);
    }
    /**
     * 商城商品列表
     *
     */
    public function mall_list()
    {
        $id=$this->request->param("id",0,'intval');
        $order="id";
        $orderWay="desc";
        $where=[];
        $where['status']=1;
        $where['item_type']=2;
        if($id>0){
            $where['cate_id']=$id;
        }
        $data=Db::name("deerhome_items")->field("id,cate_id,name,face_image,price,recommend,fake_sale_times,sale_times")->where($where)->order("{$order} {$orderWay}")->paginate(50)->each(function($item,$key){
            $item['face_image']=$this->fixImg($item['face_image'],true);
            $item['sale']="已售".($item['sale_times']+$item['fake_sale_times']);
            return $item;
        });
        $this->ok($data);
    }
    /**
     * ip转城市
     *
     */
    public function city()
    {
        $appCode=configDeer("ip2city");
        if(!isset($appCode) || $appCode==""){
            $this->ok(['city'=>'hide']);
        }
        $ip=$this->request->ip();
        $url="https://c2ba.api.huachen.cn/ip?ip=".$ip;
        $header = array();
        $header[]="Authorization: APPCODE ".$appCode;
        $header[]="Content-Type: application/json; charset=utf-8";

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, false);
        if (1 == strpos("$".$url, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        $tmpInfo = curl_exec($curl);
        if (curl_errno($curl)) {
            \think\Log::write('IP转城市失败：Errno'.curl_error($curl),'error');
            $this->err('定位失败');
        }
        curl_close($curl);
        $tmpInfo=json_decode($tmpInfo,true);
        if(!is_array($tmpInfo)){
            \think\Log::write('IP转城市失败：'.$tmpInfo,'error');
            $this->err('定位失败');
        }
        if($tmpInfo['ret']!=200){
            \think\Log::write('IP转城市失败：'.$tmpInfo['msg'],'error');
            $this->err('定位失败');
        }
        $this->ok($tmpInfo['data']);
    }
   
}
